//
//  HMVisitorView.swift
//  Weibo
//
//  Created by teacher on 2017/6/5.
//  Copyright © 2017年 teacher. All rights reserved.
//

import UIKit
import SnapKit

//  定义代理协议
protocol HMVisitorViewDelegate: NSObjectProtocol {
    //  声明登录操作的函数
    func loginOperation()
}

//  自定义访客视图
class HMVisitorView: UIView {
    
    //  接收可以执行登录的闭包
    var loginCallBack: (()->())?
    
    //  接收设置的代理
    weak var delegate: HMVisitorViewDelegate?

    //  MARK:   --懒加载控件
    //  fileprivate: 也是私有的意思，但是可以在extension里面访问，但是private不可以
    //  旋转视图
    fileprivate lazy var cycleImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //  罩层图片
    fileprivate lazy var maskIconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //  主页图片
    fileprivate lazy var homeIconImageView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //  消息文本
    fileprivate lazy var messageLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 13)
        label.textColor = UIColor.darkGray
        //  多行显示
        label.numberOfLines = 0
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        //  设置居中
        label.textAlignment = .center
        return label
    }()
    //  注册
    fileprivate lazy var registerButton: UIButton = {
        let button = UIButton()
        //  添加点击事件
        button.addTarget(self, action: #selector(registerButtonAction), for: .touchUpInside)
        button.setTitle("注册", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        let image = UIImage(named: "common_button_white_disable")!
        //  根据顶部和左边开始拉伸图片
//        let stretchImage = image.stretchableImage(withLeftCapWidth: 5, topCapHeight: 5)
        //  根据四边拉伸图片
        let stretchImage = image.resizableImage(withCapInsets: UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5), resizingMode: .stretch)
        //  设置背景图片
        button.setBackgroundImage(stretchImage, for: .normal)
        //  去掉点击高亮效果
        button.adjustsImageWhenHighlighted = false
        return button
    }()
    
    //  登录
    fileprivate lazy var loginButton: UIButton = {
        let button = UIButton()
        //  添加点击事件
        button.addTarget(self, action: #selector(loginButtonAction), for: .touchUpInside)
        button.setTitle("登录", for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        let image = UIImage(named: "common_button_white_disable")!
        //  根据顶部和左边开始拉伸图片
        //        let stretchImage = image.stretchableImage(withLeftCapWidth: 5, topCapHeight: 5)
        //  根据四边拉伸图片
        let stretchImage = image.resizableImage(withCapInsets: UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5), resizingMode: .stretch)
        //  设置背景图片
        button.setBackgroundImage(stretchImage, for: .normal)
        //  去掉点击高亮效果
        button.adjustsImageWhenHighlighted = false
        return button
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        //  如果使用xib加载这个视图会有错误提示， 我们没有实现xib
        fatalError("init(coder:) has not been implemented")
    }
    
    //  添加控件设置约束
    private func setupUI() {
        
        backgroundColor = UIColor(white: 237 / 255, alpha: 1)
        
        //  添加控件
        addSubview(cycleImageView)
        addSubview(maskIconImageView)
        addSubview(homeIconImageView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        cycleImageView.snp.makeConstraints { (make) in
            make.center.equalTo(self)
        }
        
        maskIconImageView.snp.makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
        
        homeIconImageView.snp.makeConstraints { (make) in
            make.center.equalTo(cycleImageView)
        }
        
        messageLabel.snp.makeConstraints { (make) in
            make.top.equalTo(cycleImageView.snp.bottom)
            make.centerX.equalTo(cycleImageView)
            make.width.equalTo(224)
        }
        
        registerButton.snp.makeConstraints { (make) in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp.bottom).offset(10)
            make.size.equalTo(CGSize(width: 80, height: 35))
        }
        
        loginButton.snp.makeConstraints { (make) in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.size.equalTo(registerButton)
        }
        
        
        
    }
    
    //  开启旋转视图动画
    private func startAnimation() {
        
        //  创建基本动画对象
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //  设置动画的目的地
        //  swift  3.0以后修改成这样的
        animation.toValue = 2 * Double.pi
        //  设置时长
        animation.duration = 20
        //  次数, 设置最大值
        animation.repeatCount = MAXFLOAT
        //  切换页面的时候不让其删除动画
        animation.isRemovedOnCompletion = false
        
        //  动画要在layer执行
        cycleImageView.layer.add(animation, forKey: nil)
        
    }
    
    //  修改访客视图的信息
    //  1. 可以在加一个是否是首页参数
    //  2. 可以根据可选类型判断
    func updateVisitorViewInfo(imageName: String?, message: String?) {
        if let imgName = imageName, let msg = message {
            //  不是首页
            homeIconImageView.image = UIImage(named: imgName)
            messageLabel.text = msg
            //  隐藏旋转视图
            cycleImageView.isHidden = true
        } else {
            //  是首页，开启动画
            startAnimation()
        }
    }
    
    //  MARK:   -- 登录和注册按钮点击事件处理
    @objc private func registerButtonAction() {
        print("register")
        //  执行闭包完成回调
        loginCallBack?()
        //  通过代理执行代理方法完成回调
        delegate?.loginOperation()
    }
    
    @objc private func loginButtonAction() {
        print("login")
        //  执行闭包完成回调
        loginCallBack?()
        //  通过代理执行代理方法完成回调
        delegate?.loginOperation()
    }
   

}

extension HMVisitorView {
    func test() {
        //  手写代码添加autolayout->以后自己封装控件，需要手写代码进行布局，大牛必备技能
        cycleImageView.translatesAutoresizingMaskIntoConstraints = false
        
        //  创建水平centerx的约束
        let cycleCenterXConstraint = NSLayoutConstraint(item: cycleImageView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0)
        let cycleCenterYConstraint = NSLayoutConstraint(item: cycleImageView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0)
        //  添加约束
        addConstraint(cycleCenterXConstraint)
        addConstraint(cycleCenterYConstraint)

    }
}
